# clean workspace
rm(list = ls())
# clean garbadge
gc()
# clear graphics device
# dev.off()
# set decimals to digits instead of scientific
options(scipen = 999)
# set timeout limit (laxed)
options(timeout = 10000)
# set work directory
setwd(dir = "~/Desktop/Mechanical Pollster/Replication_Material_PS/")

# load utils
library(data.table)
library(questionr)
library(tools)

# Sample function is useful but buggy -
# if you specify a single integer it returns a sequence up to that integer
sample = function(x, size, replace = F, prob = NULL) {
  if (length(x) == 1) return(x)
  base::sample(x, size = size, replace = replace, prob = prob)
}


# lib to get latex code for tables 
library(kableExtra)

for(dep.var in c('vote2024')){
  pred.files <- dir()
  dates <- gsub(".*_(\\d{8})\\.RData", "\\1",pred.files[grepl('crosstabs_2024',pred.files)])
  
  for(date in dates){
    
    load(file = paste0(date))
    
    if(dep.var == 'vote2024'){lab.order <- c('D','R','K','G','W','L','stay home','turnout')}
    
    for(c in names(crosstab.pred_list)[!names(crosstab.pred_list) %in% c("state_electoral.college",'state_simple','vote2022')]){
      
      
      if(c == 'date'){
        tmp <- as.matrix(round(100*t(apply(crosstab.pred_list[[c]][,..lab.order],2,quantile,c(0.05,0.5,0.95))),1))
        val.table <- 
          data.table(
            `Pop.` = ifelse(lab.order %in% c('turnout','stay home'),'A','LV'),
            `Vote2024` = 
              ifelse(lab.order == 'D','Harris (D)',
                     ifelse(lab.order == 'R','Trump (R)',
                            ifelse(lab.order == 'K','RFK Jr (Ind)',
                                   ifelse(lab.order == 'G','Stein (G)',
                                          ifelse(lab.order == 'L','Oliver (L)',
                                                 ifelse(lab.order == 'W','West (Ind)',
                                                        ifelse(lab.order == 'stay home','Abstention',
                                                               ifelse(lab.order == 'turnout','Turnout',
                                                                      'Other'
                                                               ) ) ) ) ) ) ) ),
            `Topline` = paste0(tmp[,'50%'],' ( ',tmp[,'5%'],' , ',tmp[,'95%'],' )')
          )
        #print(c)
      }else{
        #print(c)
        val.table <- 
          data.table(
            `Pop.` = ifelse(lab.order %in% c('turnout','stay home'),'A','LV'),
            `Vote2024` =        
              ifelse(lab.order == 'D','Harris (D)',
                     ifelse(lab.order == 'R','Trump (R)',
                            ifelse(lab.order == 'K','RFK Jr. (Ind)',
                                   ifelse(lab.order == 'G','Stein (G)',
                                          ifelse(lab.order == 'L','Oliver (L)',
                                                 ifelse(lab.order == 'W','West (Ind)',
                                                        ifelse(lab.order == 'stay home','Abstention',
                                                               ifelse(lab.order == 'turnout','Turnout',
                                                                      'Other'
                                                               ) ) ) ) ) )))
          )
        print.levels <- levels(as.factor(crosstab.pred_list[[c]][[1]]))
        if(c=='vote2020'){
          print.levels <- c('D','R','L','G','OTHER','stay home')
        }
        for(k in print.levels){
          tmp <- crosstab.pred_list[[c]]
          tmp <- as.matrix(round(100*t(apply(tmp[tmp[[c]]==k][,..lab.order],2,quantile,c(0.05,0.5,0.95))),1))
          estimate <- paste0(tmp[,'50%'],' ( ',tmp[,'5%'],' , ',tmp[,'95%'],' )')
          tmp <- data.table(temp = estimate)
          names(tmp)[names(tmp)=='temp'] <- 
            ifelse(c %in% c('age_bins','hh_income'),
                   gsub('25000','25k',
                        gsub('50000','50k',
                             gsub('75000','75k',
                                  gsub('100000','100k',
                                       gsub('max\\]','+\\)',
                                            gsub('min','0',
                                                 gsub(".*?\\.", "",k)
                                            ) ) ) ) ) ),
                   toTitleCase(k))
          val.table <- cbind(val.table,tmp)
        }
      }
      caption = 
        ifelse(
          c=='date',paste0('Topline estimates of 2024 Vote Choice, ',
                           gsub('_',' to ',gsub('-','/',gsub('2024-','',gsub('.RData','',gsub('crosstabs_','',date))))),'.'),
          paste0('Estimates of 2024 Vote Choice by ',
                 toTitleCase(gsub('vote2020','2020 Vote Choice',
                                  gsub('\\_',' ',
                                       paste(gsub('hh_income','Houshold Income',
                                                  gsub('age_bins','Age',
                                                       gsub('gender','Sex',
                                                            gsub('ethnicity','Race/Ethnicity',
                                                                 gsub('grad','grad.',c,'.'))))))))),
                 ', ',
                 gsub('_',' to ',gsub('-','/',gsub('2024-','',gsub('.RData','',gsub('crosstabs_','',date)))))
                 
          ))
      
      
      latex <- 
        kbl(val.table,format = 'latex',booktabs = T,caption = caption) %>% 
        kable_styling(latex_options = c("striped","scale_down"))
      
      latex <- gsub('\\\\else\\\\width\\\\fi', '\\\\else 0.55\\\\textwidth\\\\fi', latex)
      
      latex <- gsub('\\\\toprule', '\\\\hline 
\\\\hline', latex)
      latex <- gsub('\\\\midrule', '\\\\hline',latex) 
      latex <- gsub('\\\\bottomrule', '\\\\hline 
\\\\hline', latex)      
      latex <- gsub('\\\\begin\\{table\\}','\\\\begin\\{table\\}\\[htp!\\]',latex)      
      
      print(latex)
      
      if(c %in% c('date','gender','ethnicity') & gsub('crosstabs_','',gsub('.RData','',date))!='2024-08-15_2024-09-12'){
        writeLines(latex, paste0('generated_tables/latex.code_',c,'_',gsub('crosstabs_','',gsub('.RData','',date)),".txt"))
      }
      
    } } }
